//
//  RBMyCollectGroupVC.m
//  zhongzhenglu
//
//  Created by 马军凯 on 2023/3/21.
//

#import "RBMyCollectGroupVC.h"
#import "RBMyCollectResponse.h" //model
#import "RBHistoryCell.h" //cell
#import "RBGroupDetailVC.h" //商品详情

@interface RBMyCollectGroupVC () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) RBMyCollectGroupResponse *response;
@end

@implementation RBMyCollectGroupVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
    [self.listView.mj_header beginRefreshing];
}

#pragma mark - Private

- (void)setup
{
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData {
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
        group;
    })];
    [self appendData];
}

- (void)appendData {
    
    __weak typeof(self) weak_self = self;
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.lastObject;
        (NSMutableArray *)group.cellModels;
    });
    for (RBHistoryModel *model in self.response.data) {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBHistoryCell.class);
            cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                RBGroupDetailVC *vc = [RBGroupDetailVC new];
                vc.id = model.good_id;
                [self.navigationController pushViewController:vc animated:YES];
            };
            cellModel.cellData = model;
            cellModel;
        })];
    }
    [self.tableView reloadData];
    if(!arrayM.count)
    {
        [self.tableView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}

#pragma mark - Network
- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
    dicM[@"type"] = @"2";
    return dicM;
}
- (NSString *)listRequestPath
{
    return @"hu_collect";
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBMyCollectGoodsResponse.class;
}
- (UIScrollView *)listView
{
    return self.tableView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.listView showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}
- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(id data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}
#pragma mark - UITableViewDataSource & Delegate

kRBTableViewDataSourceAndDelegate(self.dataSource);
#pragma mark - Event

#pragma mark - Setter

#pragma mark - Getter

- (UITableView *)tableView
{
    if (!_tableView)
    {
        self.tableView = ({
            UITableView *view = [UITableView.alloc initWithFrame:CGRectZero style:UITableViewStylePlain];
            view.dataSource = self;
            view.delegate = self;
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            kRBIgnoreScrollViewInsetsAdjust(view);
            view.separatorStyle = UITableViewCellSeparatorStyleNone;
            view.tableFooterView = UIView.new;
            view.backgroundColor = UIColor.clearColor;
            view.estimatedRowHeight = UITableViewAutomaticDimension;
            [self.view addSubview:view];
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.top.mas_equalTo(Zoom(5));
                make.bottom.mas_equalTo(-Zoom(5));
            }];
            view;
        });
    }
    return _tableView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

@end
